
package hasthi.wsdm.capabilities;

import static hasthi.common.constants.HasthiConstants.*;
import static hasthi.common.constants.WSDMConstants.MANAGEMENT_EVENT_SINK_PARAM;
import static hasthi.common.constants.WSDMConstants.MANAGMENT_EVNET_SINK_PARAM;
import hasthi.common.HasthiException;
import hasthi.container.notifier.ManageableResourceNotifier;
import hasthi.wsdm.Capability;
import hasthi.wsdm.CapabilityDescription;
import hasthi.wsdm.ManagableResource;
import hasthi.wsdm.ManagedSystemHandle;
import hasthi.wsdm.ManagementAgentContext;

import javax.xml.namespace.QName;

/**
 * This directly set or get configuration from System handle
 * @author Srinath Perera(hperera@cs.indiana.edu)
 */

public class ConfigurationCapability implements Capability{
    protected CapabilityDescription capabilityDescription;
    protected ManagableResource ownedResource;
    protected ManagementAgentContext context;
    
    
    public void init(ManagementAgentContext context, ManagableResource ownedResource, CapabilityDescription description) throws HasthiException {
        this.capabilityDescription = description;
        this.ownedResource = ownedResource;
        this.context = context;
        setProperty(MANAGMENT_EVNET_SINK_PARAM, UNSPECIFIED_EPR);
    }

    public CapabilityDescription getCapabilityDescription() {
        return capabilityDescription;
    }

    public Object getProperty(QName name) throws HasthiException {
        return ownedResource.getSystemHandle().getConfiguration(name);
    }

    public void setProperty(QName name, Object value) throws HasthiException {
        if(MANAGMENT_EVNET_SINK_PARAM.equals(name)){
            String oldEventSink = (String)ownedResource.getSystemHandle().getConfiguration(MANAGMENT_EVNET_SINK_PARAM);
            ownedResource.removeNotifier(oldEventSink);
            if(!value.equals(UNSPECIFIED_EPR)){
                ownedResource.addNotifier(new ManageableResourceNotifier(context,ownedResource,
                                (String)value));    
            }
        }
        ownedResource.getSystemHandle().setConfiguration(name, value);
    }
    
    public void parentResourceInitialized() {}

    public void stop() {}
    
}


